package com.thread.controller;

import com.thread.service.AsynService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.*;

/**
 * @Author: 胡成
 * @Version: 0.0.1V
 * @Date: 2018/10/31
 * @Description: 类描述
 **/
@Slf4j
@RestController
public class AsynController {

    @Autowired
    private AsynService asynService;

    @RequestMapping("/")
    public String submit() {
        log.info("start submit");
        asynService.executeAsync();
        log.info("end submit");
        return "success";
    }

    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(1);
        service.execute(() -> {
            System.out.println("定长线程池,控制最大并发数");
        });
//
//        ScheduledExecutorService service2 = Executors.newScheduledThreadPool(1);
//        service2.schedule(() -> {
//            System.out.println("定长线程池,控制周期性执行任务");
//        },3, TimeUnit.SECONDS);

//        ExecutorService service3 = Executors.newSingleThreadExecutor();
//        final int[] index = {1};
//        for (int i = 0; i < 10; i++) {
//            service3.execute(()->{
//                System.out.println("单线程化的线程池:"+ index[0]);
//                try {
//                    Thread.sleep(2000);
//                    index[0]++;
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            });
//        }

//        ExecutorService service = Executors.newCachedThreadPool();
//
//        for (int i = 0; i < 10; i++) {
//            final int index = i;
//
//            try {
//                Thread.sleep(index * 1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//
//            service.execute(()->{
//                System.out.println(index);
//            });
//
//        }



    }
}
